using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
using octopus_project.input;
using octopus_project.scene;
using octopus_project.tileset;
using octopus_project.enumeration;
using octopus_project.screen;
using octopus_project.menu;
using octopus_project.scene.cannon.weapon;
using octopus_project.scene.build.tetrad;

namespace octopus_project {
    class Game1 : Microsoft.Xna.Framework.Game {
        public static Game instance; 

        GraphicsDeviceManager graphics;

        public static Dictionary<PlayerIndex, Player> Players;
        public static TileMap_GameplayEntry battlegroundMap;

        ScreenManager screenManager;
        Song gameSong;
        bool songstart = false;

        public Game1() {
            instance = this;
            Content.RootDirectory = "Content";

            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = Constant.WIDTH_RESOLUTION;
            graphics.PreferredBackBufferHeight = Constant.HEIGTH_RESOLUTION;
            //graphics.IsFullScreen = true;
            // Create the screen manager component.
            screenManager = new ScreenManager(this);

            Components.Add(screenManager);

            // Activate the first screens.
            screenManager.AddScreen(new BackgroundScreen());
            screenManager.AddScreen(new MainMenuScreen(screenManager));

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize() {
            Players = new Dictionary<PlayerIndex, Player>();

            Player playerOne = new Player("Omplog");
            playerOne.PlayerIndex = PlayerIndex.One;
            playerOne.Color = PlayersColor.RED;
            playerOne.PlayerKeysMap = KeyMap.playerOneKeys;
            playerOne.PlayerButtonsMap = KeyMap.playerOneButtons;

            Player playerTwo = new Player("Bana");
            playerTwo.PlayerIndex = PlayerIndex.Two;
            playerTwo.Color = PlayersColor.YELLOW;
            playerTwo.PlayerKeysMap = KeyMap.playerTwoKeys;
            playerTwo.PlayerButtonsMap = KeyMap.playerTwoButtons;

            /*Player playerThree = new Player("Alpine");
            playerThree.PlayerIndex = PlayerIndex.Three;
            playerThree.Color = PlayersColor.GREEN;
            playerThree.PlayerKeysMap = KeyMap.playerThreeKeys;
            playerThree.PlayerButtonsMap = KeyMap.playerThreeButtons;


            Player playerFour = new Player("Buchy");
            playerFour.PlayerIndex = PlayerIndex.Four;
            playerFour.Color = PlayersColor.MAGENTA;
            playerFour.PlayerKeysMap = KeyMap.playerFourKeys;
            playerFour.PlayerButtonsMap = KeyMap.playerFourButtons;*/

            Players.Add(PlayerIndex.One, playerOne);
            Players.Add(PlayerIndex.Two, playerTwo);
            //Players.Add(PlayerIndex.Three, playerThree);
           // Players.Add(PlayerIndex.Four, playerFour);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content. 
        /// </summary>
        protected override void LoadContent() {

            TileTypeExtensions.SpriteImage = Content.Load<Texture2D>(@"image/part1_tileset2");
            WarPhaseSpriteExtensions.SpriteImage = Content.Load<Texture2D>(@"image/war_sprites");
            SpriteExtensions.SpriteImage = Content.Load<Texture2D>(@"image/sprites");
            
            gameSong = Content.Load<Song>(@"sound/indy");
            MiniCannon.soundEffect = Content.Load<SoundEffect>(@"sound/bomb");
            MediumCannon.soundEffect = Content.Load<SoundEffect>(@"sound/bomb");

            for (int i = 0; i < Tetrad.builtSound.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        Tetrad.builtSound[0] = Content.Load<SoundEffect>(@"sound/wall");
                        break;
                    case 1:
                        Tetrad.builtSound[1] = Content.Load<SoundEffect>(@"sound/wall2");
                        break;
                    case 2:
                        Tetrad.builtSound[2] = Content.Load<SoundEffect>(@"sound/wall3");
                        break;
                }

            }

            base.LoadContent();

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent() {
            // TODO: Unload any non ContentManager content here
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime) {
            if(!songstart) {
                MediaPlayer.Play(gameSong);
                songstart = true;
            }  
            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.Black);
            base.Draw(gameTime);
        }

    }
}
